#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <pthread.h>
#include <mqueue.h>

#include <openssl/opensslv.h>

int main()
{
    // os
#if defined(__linux__)
    printf("Linux\n");
#endif
#if defined(__FreeBSD__)
    printf("FreeBSD\n");
#endif
#if defined(__APPLE__)
    printf("Apple\n");
#endif
#if defined(__Solaris__)
    printf("Solaris\n");
#endif
    printf("\n");

    // cpu
#if defined(__x86_64__)
    printf("x86_64\n");
#endif
    printf("\n");

    // endian
#if BYTE_ORDER == LITTLE_ENDIAN
    printf("Little Endian\n");
#else
    printf("Big Endian\n");
#endif
    printf("\n");

    // openssl
#if (OPENSSL_VERSION_NUMBER >> 12) == 0x00906L
    printf("openssl 0.9.6\n");
#elif (OPENSSL_VERSION_NUMBER >> 12) == 0x00907L
    printf("openssl 0.9.7\n");
#elif (OPENSSL_VERSION_NUMBER >> 12) == 0x00908L
    printf("openssl 0.9.8\n");
#elif (OPENSSL_VERSION_NUMBER >> 12) > 0x00908L
    printf("openssl > 0.9.8\n");
#endif
    printf("\n");

    // posix thread
    pthread_attr_t thrd_attr;
    size_t stack_size;
    pthread_attr_init(&thrd_attr);
    pthread_attr_getstacksize(&thrd_attr, &stack_size);
    printf("thrd: stacksize = %d\n", (int)stack_size);
    if(pthread_attr_setstacksize(&thrd_attr, 64*1024))
    {
        printf("pthread_attr_setstacksize fail\n");
        return(-1);
    }
    pthread_attr_getstacksize(&thrd_attr, &stack_size);
    printf("thrd: reset stacksize -> %d\n", (int)stack_size);
    printf("pid_t: %ld\n", (long)sizeof(pid_t));
    printf("\n");

    //printf("file_offset_bits: %d\n", _FILE_OFFSET_BITS);

    // posix message queue
    char msgq[100];
    strcpy(msgq, "/msgq_test");
    if(mq_unlink(msgq))
    {
        if(errno != ENOENT)
        {
            fprintf(stderr, "delete %s fail, errno:%d\n", msgq, errno);
            return(1);
        }
        fprintf(stderr, "%s ENOENT\n", msgq);
    }

    int oflag=(O_CREAT | O_RDWR);
    int mode=(S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
    struct mq_attr attr;
    attr.mq_flags=0;
    attr.mq_maxmsg=200;
    attr.mq_msgsize=1024*1024;
    attr.mq_curmsgs=0;
    mqd_t fd=mq_open(msgq, oflag, mode, &attr);
    if(fd == (mqd_t)-1)
    {
        fprintf(stderr, "open queue fail, errno:%d\n", errno);
        return(1);
    }
    printf("posix mqueue(%s) create succeed\n", msgq);
    memset(&attr, 0, sizeof(attr));
    mq_getattr(fd, &attr);
    printf("flags:%ld maxmsg:%ld msgsize:%ld curmsgs:%ld\n",
            attr.mq_flags, attr.mq_maxmsg, attr.mq_msgsize, attr.mq_curmsgs);
    mq_close(fd);
    mq_unlink(msgq);

    return(0);
}
